package com.example8.dynamic_datasource.config;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 动态数据源配置
 * @Author lihw
 * @Date 2024/3/28 11:36
 */
@Slf4j
@Component
public class DynamicDataSourceConfig {

    @Bean
    public DynamicRoutingDataSource dynamicDataSource(CustomDynamicDataSourceProvider customDynamicDataSourceProvider) {

        // 创建DynamicDataSourceProvider实例列表
        List<DynamicDataSourceProvider> providers = new ArrayList<>();
        providers.add(customDynamicDataSourceProvider); // 添加您的DynamicDataSourceProvider实现

        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource(providers);

        return dynamicDataSource;
    }

}
